############################## Daidze XML Wrapper #######################################
# Daidze XML Wrapper is part of "Daidze Software" developed by Mind System Connection.Lda 
# and Maksym Zinchenko (Siqsuruq)
# 2008-2013 (c) All Rights Reserved
#
#
#
# Data Handlers Abreviations:
#
# ***.e - Entry
# ***.bc - Barcode
# ***.b - Boolean value
# ***.tbl - Table
# ***.ebin - File (For binary data, file location selector handler, yet to check)
# ***.dir - Directory (Directory selector)
# ***.dd - Drop down menu
# ***.lb - ListBox
# ***.sb - SpinBox
# ***.date - Date
# ***.txt -Text
# ***.picture - Static Image Placer
# ***.time - TimeBox selector
# ***.clock - Clock
#
# ***.bt - Button (Deactivated)
# ***.lbt - Labeled button (Deactivated)
# ***.menu - Menu
# ***.plan - Planer
#########################################################################################









oo::class create xml_wrapper {
	
	constructor {args} {
		my variable lsize
		set lsize 20
		my variable esize
		set esize 20
		my variable width
		set width 10
		my variable height
		set height 10
		my variable justify
		set justify left
		
		my variable img
		set img [file join $::icons_dir $::icons_in_use]
		my variable mod_dir
		set mod_dir $::mod_dir
		
		# Data handlers: fdata - dict used to get form data, sdata - initial values of form
		my variable fdata
		set fdata [dict create]
		my variable sdata
		set sdata [dict create]
	}

	method load_main {folder xmlfile fr args} {
		my variable mod_dir
		set fol [file join $mod_dir $folder]
		my Destroy_main $fr
		my Xml_to_fr [file join $fol $xmlfile] $fr
	}

	method Destroy_main {fr} {
		set pack_options [lreplace [pack info $fr] 0 1]
		set frame_options [list]
		foreach a [$fr config] {
			lappend frame_options [lindex $a 0] 
			lappend frame_options [$fr cget [lindex $a 0]]
		}
		destroy $fr
		pack [ttk::frame $fr {*}$frame_options] {*}$pack_options
	}


	method Xml_to_fr {xml_file w} {
		my variable fdata

		set doc [dom parse [tDOM::xmlReadFile $xml_file]]
		set root [$doc getElementsByTagName form]
		set nodes [$root childNodes]

		if {[info exists fdata] == 1} {unset fdata}
		my Explore $root $w
	}

################################################################################################################################################################
	method Explore {parent w} {
		my Widget $parent [$parent ancestor all] $w
		foreach child [$parent childNodes] {
			my Explore $child $w
		}
	}







method Widget {node parent w} {
	my variable img
	my variable fdata
	my variable height
	my variable width
	my variable sdata


	set parentnode [join [lreverse $parent] "."]

	if {[$node nodeName] == "form" } { 
		pack [ttk::frame $w.$node] -fill both -padx 1 -pady 1 -expand 1
	} elseif { [$node nodeName] == "container" } {
		if {[$node getAttribute pos] == "ver"} {
			set ::side "left"
		} else {
			set ::side "top"
		}
		if {[$node getAttribute expand] == "yes"} {
			set ::expand 1
		} else { set ::expand 0 }

		if {[$node getAttribute type] == "frame"} {
			pack [ttk::frame $w.$parentnode.$node] -fill both -padx 2 -pady 2 -expand $::expand  -side $::side
    	} elseif {[$node getAttribute type] == "labelframe"} {
			pack [ttk::labelframe $w.$parentnode.$node -text [::msgcat::mc [$node getAttribute label]]] -fill both -padx 2 -pady 2 -expand $::expand -side $::side
		} elseif {[$node getAttribute type] == "notebook"} {
			pack [ttk::notebook $w.$parentnode.$node] -fill both -padx 2 -pady 2 -expand $::expand -side $::side
			ttk::notebook::enableTraversal $w.$parentnode.$node
			set r [$node childNodes]
			foreach tab $r {
				set l [$tab childNodes]
				$w.$parentnode.$node add [ttk::frame $w.$parentnode.$node.$tab] -text [::msgcat::mc "[$tab getAttribute label]"]
				pack [ttk::frame $w.$parentnode.$node.$tab.tabfr] -expand 1 -fill both
				set d [set ::[$tab getAttribute var] [xml_wrapper new]]
				set modul_name [lindex [$l nodeValue] 0]
				set xml_file [lindex [$l nodeValue] 1]
				$d load_main $modul_name $xml_file.xml $w.$parentnode.$node.$tab.tabfr 1	
			}
    	} elseif {[$node getAttribute type] == "scrolledframe"} {
			set r [$node childNodes]
			# pack [set d [ttk::frame $w.$parentnode.$node]] -expand 1 -fill both
			scrolledframe $w.$parentnode.$node -yscrollcommand [list $w.$parentnode.$node.vsb set] -xscrollcommand [list $w.$parentnode.$node.hsb set]
			# pack [ttk::frame $w.$parentnode.$node.scrolled] -expand 1 -fill both
			pack $w.$parentnode.$node -side left -pady 30 -padx 30
			pack [ttk::scrollbar $w.$parentnode.$node.vsb -orient vertical -command [list $w.$parentnode.$node yview]] -fill y -side right
			pack [ttk::scrollbar $w.$parentnode.$node.hsb -orient horizontal -command [list $w.$parentnode.$node xview]] -fill x


			$w.$parentnode.$node configure -bg green
		}

	} elseif {[$node nodeName] == "line"} {
		pack [ttk::separator $w.$parentnode.$node] -fill x -padx 2 -pady 2


	} elseif {[$node nodeName] == "label"} {
		set r [$node childNodes]
		pack [ttk::label $w.$parentnode.$node -text [::msgcat::mc "[$r nodeValue]"]] -fill both -padx 5 -pady 5


	} elseif {[$node nodeName] == "label_button"} {
		set r [$node childNodes]
		set d [ttk::frame $w.$parentnode.$node]
		set lsize [my Common_attr $node lsize]
		set width [my Common_attr $node width]

		pack [ttk::label $d.l -text [::msgcat::mc "[$r nodeValue]"] -width $lsize] -fill both -padx 2 -pady 2 -side left

		if  {[$node hasAttribute img] == 0} {
			pack [ttk::button $d.lbt -text [::msgcat::mc "[$node getAttribute btext]"] -command [$node getAttribute cmd] -width $width] -side left -padx 2 -pady 2
		} else { pack [ttk::button $d.lbt -image [image create photo -file [file join $img [$node getAttribute img]]] -command [$node getAttribute cmd]] -side left -padx 2 -pady 2 }
		pack $d -side top -pady 2 -padx 2 -fill both


	} elseif {[$node nodeName] == "entry"} {
		set r [$node childNodes]
		set d [ttk::frame $w.$parentnode.$node]
		set lsize [my Common_attr $node lsize]
		set esize [my Common_attr $node esize]
		set justify [my Common_attr $node justify]

		pack [ttk::label $d.l -text [::msgcat::mc "[$r nodeValue]"] -width $lsize] -fill both -padx 2 -pady 2 -side left

		if {[$node hasAttribute state] == 0} {
			if {[$node hasAttribute type] != 0 && [$node getAttribute type] == "password"} {
				pack [ttk::entry $d.e -width $esize -show *] -side left
			} else {
				if {[$node hasAttribute mandatory]!= 0 && [$node getAttribute mandatory] == 1} {
					ttk::entry $d.e -width $esize -foreground red -textvariable [$node getAttribute var] -justify $justify
				} else {
					ttk::entry $d.e -width $esize -textvariable [$node getAttribute var] -justify $justify
				}
				pack $d.e -side left
			}
		} else {
			pack [ttk::entry $d.e -width $esize -state disabled -justify $justify] -side left 
		}

		pack $d -side top -pady 2 -padx 2 -fill both
		dict append fdata [$node getAttribute var] $d.e
		if {[$node hasAttribute tip] != 0} {
			tooltip::tooltip $d.e "[::msgcat::mc "[$node getAttribute tip]"]"
		}
		my WidgetBind $node $d.e
		
	} elseif {[$node nodeName] == "barcode"} {
		set r [$node childNodes]
		set d [ttk::frame $w.$parentnode.$node]
		set lsize [my Common_attr $node lsize]
		set esize [my Common_attr $node esize]

		pack [ttk::label $d.l -text [::msgcat::mc "[$r nodeValue]"] -width $lsize] -fill both -padx 2 -pady 2 -side left

		if {[$node hasAttribute state] == 0} {
			if {[$node hasAttribute type] != 0 && [$node getAttribute type] == "password"} {
				pack [ttk::entry $d.bc -width $esize -show *] -side left
			} else {
				if {[$node hasAttribute mandatory]!= 0 && [$node getAttribute mandatory] == 1} {
					ttk::entry $d.bc -width $esize -foreground red -textvariable [$node getAttribute var]
					pack $d.bc -side left
				} else {
					ttk::entry $d.bc -width $esize -textvariable [$node getAttribute var]
					pack $d.bc -side left
					focus $d.bc
				}
			}
		} else {
			pack [ttk::entry $d.bc -width $esize -state disabled] -side left
			focus $d.bc
		}

		pack $d -side top -pady 2 -padx 2 -fill both
		dict append fdata [$node getAttribute var] $d.bc
		
		# This will execute command after scan
		if {[$node hasAttribute onScan] != 0} {
			bind $d.bc <Return> [list [$node getAttribute onScan] %W]
		}

	} elseif {[$node nodeName] == "bool"} {
		set r [$node childNodes]
		set d [ttk::frame $w.$parentnode.$node]
		set lsize [my Common_attr $node lsize]

		pack [ttk::label $d.l -text [::msgcat::mc "[$r nodeValue]"] -width $lsize] -side left -padx 2 -fill y
		pack [ttk::checkbutton $d.b -variable [namespace current]::[$node getAttribute var] -onvalue 1] -side left -padx 2 -fill y
		set [namespace current]::[$node getAttribute var] 0

		pack $d -side top -pady 2 -padx 2 -fill both
		dict append fdata [$node getAttribute var] $d.b
		
		
	} elseif {[$node nodeName] == "radio"} {
		set r [$node childNodes]
		set d [ttk::frame $w.$parentnode.$node]
		set lsize [my Common_attr $node lsize]

		pack [ttk::radiobutton $d.radiob -variable [namespace current]::[$node getAttribute var] -text [::msgcat::mc "[$r nodeValue]"] -width $lsize -value "[$node getAttribute value]"] -side left -padx 2 -fill y
		# set [$node getAttribute var] 0

		pack $d -side top -pady 2 -padx 2 -fill both
		dict set fdata [$node getAttribute var] $d.radiob
		
		
	} elseif {[$node nodeName] == "table"} {
		set r [$node childNodes]
		set headers {}
		set col_val {}
		
		if {[$node hasAttribute headers_type] != 0 && [$node getAttribute headers_type] == "dict"} {
			set tmp_dict [$node getAttribute headers]
			foreach t [dict keys $tmp_dict] u [dict values $tmp_dict] {
				lappend headers [::msgcat::mc "$u"]
				lappend col_val [::msgcat::mc "$t"]
			}
		} else {
			foreach t [$node getAttribute headers] {
				lappend headers [::msgcat::mc "$t"]
			}
		}
		
		if {[dict size $sdata] != 0} {
			set result [dict create]
		} else {
			if {[$r nodeValue] != "empty"} {
				if {$col_val != ""} {
					set result [$::db select_all [$r nodeValue] $col_val]
				} else {
					set result [$::db select_all [$r nodeValue]]
				}
			} else {
				set result [dict create]
			}
		}
		if {[$node getAttribute type] eq "func"} {
			set result [$r nodeValue]

		}
		
		pack [set d [ttk::frame $w.$parentnode.$node]] -fill both -expand 1
		if {[$node hasAttribute selectmode] != 0} {
			set selmode [$node getAttribute selectmode]
		} else {
			set selmode "single"
		}
		# Default columns sorting or from XML file

# 		set def_tbl_conf [dict create]
# 		if {$col_val ne ""} {
# 			for {set pos 0} {$pos > [llength $col_val]} {incr pos} {
# 				if {[lindex $col_val $pos] eq id} {
# 					dict append def_tbl_conf $pos {"-sortmode" integer}
# 				} elseif {[lindex $col_val $pos] eq id} {
# 				
# 				}
# 			}
# 		}
# 		puts "COLUMNS TYPES: [$::db get_columns_types [$r nodeValue]]"

		if {[$node hasAttribute sortmode] == 0} {
			set def_tbl_conf [dict create 0 {"-sortmode" integer}]
		} else {
			set def_tbl_conf [dict create]
			foreach val [$node getAttribute sortmode] {
				dict append def_tbl_conf [lindex $val 0] "\"-sortmode\" [lindex $val 1]"
			}
		}


		tablelist::tablelist $d.tbl -columntitles $headers -labelcommand tablelist::sortByColumn -showseparators yes -stripebackground #e0e8f0 -selectbackground LightSteelBlue3 -selectforeground black \
		-height 10 -width 90 -stretch all -yscrollcommand [list $d.vsb set] -xscrollcommand [list $d.tbl.hsb set] -arrowstyle flat10x6 -arrowcolor green \
		-selecttype row -selectmode $selmode 

if {[info exists col_val] != 0 && $col_val ne ""} {
	for {set u 0} {$u < [llength $col_val]} {incr u} {
		$d.tbl columnconfigure $u -name [lindex $col_val $u]
	}
}

		foreach col [dict keys $def_tbl_conf] colval [dict values $def_tbl_conf] {
			set r [dict get $def_tbl_conf $col]
			foreach b [dict keys $r] {
				$d.tbl columnconfigure $col $b [dict get $r $b]
			}
		}

		for {set i 0} {$i < [dict size $result]} {incr i} {
			set dict_values [dict values [dict get $result $i]]
			$d.tbl insert end $dict_values
			$d.tbl cellconfigure end,1 -foreground grey50 -selectforeground blue
		}
		pack $d.tbl -expand 1 -fill both -side left
		pack [ttk::scrollbar $d.vsb -orient vertical -command [list $d.tbl yview]] -fill both -side right
		pack [ttk::scrollbar $d.tbl.hsb -orient horizontal -command [list $d.tbl xview]] -fill x

		dict append fdata [$node getAttribute var] $d.tbl
		
		my WidgetBind $node $d.tbl

	} elseif {[$node nodeName] == "file"} {
		set r [$node childNodes]
		set d [ttk::frame $w.$parentnode.$node]
		set lsize [my Common_attr $node lsize]
		set esize [my Common_attr $node esize]
		if {[$node hasAttribute type] != 0} {
			set ftype [$node getAttribute type]
		} else {
			set ftype [list {"All files" {*}}]
		}
		pack [ttk::label $d.l -text [::msgcat::mc "[$r nodeValue]"] -width $lsize] -side left -padx 2 -fill y
		pack [ttk::entry $d.ebin -width $esize -textvariable [$node getAttribute var]] -side left
		pack [ttk::button $d.b -image [image create photo -file [file join $img folder.png]] -command [list [namespace current]::my Open_f $d.ebin $ftype]] -side left -padx 2 -pady 2

		pack $d -side top -pady 2 -padx 2 -fill both
		dict append fdata [$node getAttribute var] $d.ebin
		my WidgetBind $node $d.ebin
		
	} elseif {[$node nodeName] == "dir"} {
		set r [$node childNodes]
		set d [ttk::frame $w.$parentnode.$node]
		set lsize [my Common_attr $node lsize]
		set esize [my Common_attr $node esize]

		pack [ttk::label $d.l -text [::msgcat::mc "[$r nodeValue]"] -width $lsize] -side left -padx 2 -fill y
		pack [ttk::entry $d.dir -width $esize -textvariable [$node getAttribute var]] -side left
		pack [ttk::button $d.b -image [image create photo -file [file join $img folder.png]] -command [list [namespace current]::my Open_dir $d.dir]] -side left -padx 2 -pady 2

		pack $d -side top -pady 2 -padx 2 -fill both
		dict append fdata [$node getAttribute var] $d.dir

	} elseif {[$node nodeName] == "dropdown"} {
		set r [$node childNodes]
		set d [ttk::frame $w.$parentnode.$node]
		set lsize [my Common_attr $node lsize]
		set esize [my Common_attr $node esize]

		if {[$node hasAttribute sort] != 0 && [$node getAttribute sort] == "-ascii"} {
			set sort_method "-ascii"
		} else {
			set sort_method "-dictionary"
		}

		if {[$node getAttribute vtype] == "list"} {
			set ddvalues [lsort $sort_method [$node getAttribute values]]
		} elseif {[$node getAttribute vtype] == "table"} {
			set values [$node getAttribute values]
			set ddvalues [lsort $sort_method [$::db select_column [lindex $values 0] [lindex $values 1]]]
		} elseif {[$node getAttribute vtype] == "func"} {
			set func [$node getAttribute values]
			set ddvalues [lsort $sort_method [$func]]
		}
		set ddvalues [linsert $ddvalues 0 ""]			;# Adds first empty value to combobox 

		pack [ttk::label $d.l -text [::msgcat::mc "[$r nodeValue]"] -width $lsize] -side left -padx 2 -fill y
		if {[$node hasAttribute state] == 0} {
		pack [ttk::combobox $d.dd -width $esize -state readonly -values $ddvalues ] -side left
		} else {
			pack [ttk::combobox $d.dd -width $esize -state disabled -values $ddvalues ] -side left
		}
		pack $d -side top -pady 2 -padx 2 -fill both
		dict append fdata [$node getAttribute var] $d.dd

		if {[$node hasAttribute onSelect] != 0} {
			bind $d.dd <<ComboboxSelected>> [list [$node getAttribute onSelect] %W]
		}

	} elseif {[$node nodeName] == "listbox"} {
		set r [$node childNodes]
		set d [ttk::frame $w.$parentnode.$node]
		set lsize [my Common_attr $node lsize]
		set esize [my Common_attr $node esize]
		
		set height [my Common_attr $node height]
		if {[$node getAttribute vtype] == "list"} {
			set lbvalues [$node getAttribute values]
		} elseif {[$node getAttribute vtype] == "table"} {
			set values [$node getAttribute values]
			set lbvalues [$::db select_column [lindex $values 0] [lindex $values 1]]
		} elseif {[$node getAttribute vtype] == "func"} {
			set func [$node getAttribute values]
			set lbvalues [$func]
		}

		if {[$node hasAttribute framed] && [$node getAttribute framed] == "yes"} {
				pack [set inner [ttk::frame $d.inner]] -side left -fill both
				pack [ttk::labelframe $d.inner.frmlb -text [::msgcat::mc "[$r nodeValue]"] -padding 5] -side left -fill both
				pack [set lb [listbox $inner.frmlb.lb -height $height -listvariable [$node getAttribute var] -yscroll [list $inner.frmlb.scroll set] -exportselection 0 -width $esize]] -side left
				pack [ttk::scrollbar $inner.frmlb.scroll -command [list $inner.frmlb.lb yview]] -fill both -side right
		} else {
				pack [set inner [ttk::frame $d.inner]] -side left -fill both
				pack [ttk::label $inner.l -text [::msgcat::mc "[$r nodeValue]"] -width $lsize] -side left -padx 2 -fill y
				pack [set lb [listbox $inner.lb -height $height -listvariable [$node getAttribute var] -yscroll [list $inner.scroll set] -exportselection 0 -width $esize]] -side left
				pack [ttk::scrollbar $inner.scroll -command [list $inner.lb yview]] -fill both -side right
		}
		foreach e $lbvalues {
			$lb insert 0 $e
		}

		pack $d -side top -pady 2 -padx 2 -fill both
		dict append fdata [$node getAttribute var] $lb


	} elseif {[$node nodeName] == "spinbox"} {
		set r [$node childNodes]
		set d [ttk::frame $w.$parentnode.$node]
		set lsize [my Common_attr $node lsize]
		set esize [my Common_attr $node esize]

		pack [ttk::label $d.l -text [::msgcat::mc "[$r nodeValue]"] -width $lsize] -side left -padx 2 -fill y
		pack [ttk::spinbox $d.sb -from [$node getAttribute min] -to [$node getAttribute max] -width $esize -validate key -validatecommand {string is integer %P}] -side left
		pack $d -side top -pady 2 -padx 2 -fill both
		dict append fdata [$node getAttribute var] $d.sb
		my WidgetBind $node $d.sb

	} elseif {[$node nodeName] == "date"} {
		set r [$node childNodes]
		set d [ttk::frame $w.$parentnode.$node]
		set lsize [my Common_attr $node lsize]
		set esize [my Common_attr $node esize]
		set clockformat "%Y-%m-%d"

		set [$node getAttribute var] [clock format [clock seconds] -format $clockformat]
		pack [ttk::label $d.l -text [::msgcat::mc "[$r nodeValue]"] -width $lsize] -side left -padx 2 -fill y
		pack [ttk::entry $d.date -textvariable [$node getAttribute var]] -side left -padx 0 -pady 2
		pack [ttk::button $d.b -image [image create photo -file [file join $img calendar.png]] -command [list ::DATE::my_popup_Cal $d.date]] -side left -padx 2 -pady 2

		pack $d -side top -pady 2 -padx 2 -fill both
		dict append fdata [$node getAttribute var] $d.date
 
	} elseif {[$node nodeName] == "dateperiod"} {
		set r [$node childNodes]
		set d [ttk::frame $w.$parentnode.$node]
		set lsize [my Common_attr $node lsize]
		set esize [my Common_attr $node esize]
		set clockformat "%Y-%m-%d"

		set [$node getAttribute var] [clock format [clock seconds] -format $clockformat]
		pack [ttk::label $d.l -text [::msgcat::mc "[$r nodeValue]"] -width $lsize] -side left -padx 2 -fill y
		pack [ttk::entry $d.date1] -side left -padx 0 -pady 2
		pack [ttk::button $d.b1 -image [image create photo -file [file join $img calendar.png]] -command [list ::DATE::my_popup_Cal $d.date1]] -side left -padx 2 -pady 2
		pack [ttk::entry $d.date2] -side left -padx 0 -pady 2
		pack [ttk::button $d.b2 -image [image create photo -file [file join $img calendar.png]] -command [list ::DATE::my_popup_Cal $d.date2]] -side left -padx 2 -pady 2

		pack $d -side top -pady 2 -padx 2 -fill both
		dict append fdata [$node getAttribute var] $d.dateperiod

	} elseif {[$node nodeName] == "button"} {
		my variable module_id
		my variable form_id

		set r [$node childNodes]
		if {[$node hasAttribute width] == 0} {
			set width [my Common_attr $node width]
		} else {set width [$node getAttribute width]}




		if  {[$node hasAttribute img] == 0} {
			pack [ttk::button $w.$parentnode.$node -text [::msgcat::mc "[$r nodeValue]"] -command [$node getAttribute cmd] -width $width] -side $::side -padx 2 -pady 2
			tooltip::tooltip $w.$parentnode.$node "[::msgcat::mc "[$r nodeValue]"]"
		} else { 
			pack [ttk::button $w.$parentnode.$node -image [image create photo -file [file join $img [$node getAttribute img]]] -command [$node getAttribute cmd]] -side $::side -padx 2 -pady 2 
			tooltip::tooltip $w.$parentnode.$node "[::msgcat::mc "[$r nodeValue]"]"
		}


	} elseif {[$node nodeName] == "text"} {
		set r [$node childNodes]
		pack [set d [ttk::frame $w.$parentnode.$node]] -side top -pady 2 -padx 2 -fill both
		set lsize [my Common_attr $node lsize]
		set esize [my Common_attr $node esize]

		set height [my Common_attr $node height]

		if {[$node hasAttribute framed] && [$node getAttribute framed] == "yes"} {
			pack [set inner [ttk::frame $d.inner]] -side left -fill both
			pack [ttk::labelframe $d.inner.frmtb -text [::msgcat::mc "[$r nodeValue]"] -padding 5] -side left -fill both
			pack [set scrtb [ttk::frame $inner.frmtb.scrtb]]
			pack [text $scrtb.txt -wrap word -width $esize -height $height -padx 1 -pady 1 -yscrollcommand [list $scrtb.scroll set]] -side left -padx 2 -pady 2
			pack [ttk::scrollbar $scrtb.scroll -command [list $scrtb.txt yview]] -fill both -side right
		} else {
			pack [set inner [ttk::frame $d.inner]] -side left -fill both
			pack [ttk::label $inner.l -text [::msgcat::mc "[$r nodeValue]"] -width $lsize] -side left -padx 2 -fill y
			pack [set scrtb [ttk::frame $inner.scrtb]]
			pack [text $scrtb.txt -wrap word -width $esize -height $height -padx 1 -pady 1 -yscrollcommand [list $scrtb.scroll set]] -side left -padx 2 -pady 2
			pack [ttk::scrollbar $scrtb.scroll -command [list $scrtb.txt yview]] -fill both -side right
		}
		dict append fdata [$node getAttribute var] $scrtb.txt


	} elseif {[$node nodeName] == "image"} {
		set r [$node childNodes]

		set lsize [my Common_attr $node lsize]
		
		if {[$node getAttribute type] == "file"} {
			if {[$node getAttribute values] != "" && [file exist [file join $img [$node getAttribute values]]] == 1} {
				set pic [image create photo -file [file join $img [$node getAttribute values]]]
			} else {set pic [image create photo -file [file join $img no_img.png]]}
		} elseif {[$node getAttribute type] == "func"} {
			set pic [[$node getAttribute var]]
		} else {
			set pic [image create photo -file [file join $img no_img.png]]
		}

		pack [set d [ttk::frame $w.$parentnode.$node]] -side top -pady 2 -padx 2 -fill both

		if {[$node hasAttribute framed] && [$node getAttribute framed] == "yes"} {
			pack [ttk::labelframe $d.frmimg -text [::msgcat::mc "[$r nodeValue]"] -padding 5] -side left -fill both
			pack [ttk::label $d.frmimg.picture -relief flat -image $pic -textvariable [namespace current]::[$node getAttribute var]] -side left
			dict append fdata [$node getAttribute var] $d.frmimg.picture
			bind $d.frmimg.picture <Double-1> [list set_img $d.frmimg.picture]
		} else {
			pack [ttk::label $d.l -text [::msgcat::mc "[$r nodeValue]"] -width $lsize] -side left -padx 2 -fill y
			pack [ttk::label $d.picture -relief flat -image $pic -textvariable [namespace current]::[$node getAttribute var]] -side left
			dict append fdata [$node getAttribute var] $d.picture
			bind $d.frmimg.picture <Double-1> [list set_img $d.frmimg.picture]
		}


	} elseif {[$node nodeName] == "time"} {
		set r [$node childNodes]
		set d [ttk::frame $w.$parentnode.$node]
		set lsize [my Common_attr $node lsize]
		set esize [my Common_attr $node esize]

		if {[$node hasAttribute format] != 0 } {
			set t_format [$node getAttribute format]
		} else {
			set t_format "Hm"
		}
		set [namespace current]::[$node getAttribute var] [time_hm]
		pack [ttk::label $d.l -text [::msgcat::mc "[$r nodeValue]"] -width $lsize] -side left -padx 2 -fill y
		pack [ttk::timebox $d.time [namespace current]::[$node getAttribute var] $t_format  -width $esize] -side left -padx 0 -pady 2

		pack $d -side top -pady 2 -padx 2 -fill both
		dict append fdata [$node getAttribute var] $d.time

	} elseif {[$node nodeName] == "clock"} {
		set r [$node childNodes]
		set d [frame $w.$parentnode.$node]
		set lsize [my Common_attr $node lsize]
		set esize [my Common_attr $node esize]

		if {[$node hasAttribute format] != 0 } {
			set t_format [$node getAttribute format]
		} else {
			set t_format "%H:%M:%S"
		}

		set [namespace current]::[$node getAttribute var] [time_hm]
		pack [ttk::label $d.l -text [::msgcat::mc "[$r nodeValue]"] -width $lsize] -side left -padx 2 -fill y
		pack [ttk::entry $d.clock  -textvar ::daidze_time -state readonly -width $esize] -side left -padx 0 -pady 2
		every 1000 {set ::daidze_time [clock format [clock sec] -format %H:%M:%S]}

		pack $d -side top -pady 2 -padx 2 -fill both
		dict append fdata [$node getAttribute var] $d.clock
		
	 } elseif {[$node nodeName] == "menu"} {
		set r [$node childNodes]
		
		if {![winfo exists .[$node getAttribute var]]} {
			set ::m [menu .[$node getAttribute var]]
		} else {
			destroy .[$node getAttribute var]
			set ::m [menu .[$node getAttribute var]]
		}
		set cmd_dict [$node getAttribute cmd]
		foreach label_text [dict keys $cmd_dict] cmd_action [dict values $cmd_dict] {
			$::m add command -label [::msgcat::mc "$label_text"] -command $cmd_action
		}
		set attach_to [dict get $fdata [$node getAttribute attach]].body
		bind $attach_to <Button-3> {tk_popup $::m %X %Y}
		
	}  elseif {[$node nodeName] == "planer"} {
		set r [$node childNodes]
		set d [ttk::frame $w.$parentnode.$node]
		pack $d -side top -pady 2 -padx 2 -expand 1 -fill both
		
		pack [ttk::separator $d.line1] -padx 2 -pady 2
		pack [set nav_b [ttk::frame $d.nav_buttons]] -expand 0 -fill x

		pack [set cal_hold [ttk::frame $d.cal_holder]] -expand 1 -fill both
		pack [set cal_main [ttk::frame $cal_hold.cal_main]] -side left -expand 1 -fill both
		set headers [list "[::msgcat::mc "Time"]" "[::msgcat::mc "Description"]"]
		tablelist::tablelist $cal_main.tbl -columntitles $headers -labelcommand tablelist::sortByColumn -showseparators yes -stripebackground #e0e8f0 -selectbackground LightSteelBlue3 -selectforeground black \
		-height 10 -width 90 -stretch all -yscrollcommand [list $cal_main.vsb set] -xscrollcommand [list $cal_main.tbl.hsb set]  -arrowstyle flat10x6 -arrowcolor green  \
		-selecttype row -selectmode single
		pack $cal_main.tbl -expand 1 -fill both -side left
		pack [ttk::scrollbar $cal_main.vsb -orient vertical -command [list $cal_main.tbl yview]] -fill y -side right
		pack [ttk::scrollbar $cal_main.tbl.hsb -orient horizontal -command [list $cal_main.tbl xview]] -fill x

		set time_list [list 00:00 00:30 01:00 01:30 02:00 02:30 03:00 03:30 04:00 04:30 05:00 05:30 06:00 06:30 07:00 07:30 08:00 08:30 09:00 09:30 10:00 10:30 11:00 11:30 12:00 12:30 13:00 13:30 14:00 14:30 15:00 15:30 16:00 16:30 17:00 17:30 18:00 18:30 19:00 19:30 20:00 20:30 21:00 21:30 22:00 22:30 23:00 23:30]
		for {set i 0} {$i < [llength $time_list]} {incr i} {
			$cal_main.tbl insert end [lindex $time_list $i]
			$cal_main.tbl cellconfigure end,1 -foreground grey50 -selectforeground blue
		}

		pack [ttk::label $nav_b.working_date_label -text "[::msgcat::mc "Current date:"]"] -side left -padx 1 -pady 0
		set [namespace current]::working_date [date]
		set [namespace current]::working day
		pack [ttk::label $nav_b.lbl -textvariable [namespace current]::working_date] -side left -padx 1 -pady 0
		
		pack [ttk::separator $d.line2] -fill x -padx 2 -pady 2

		pack [set mini_cal [ttk::frame $cal_hold.cal_mini]] -side top -padx 5 -pady 5
		pack [ttk::separator $cal_hold.line1 -orient vertical] -side right -fill y
		pack [widget::calendar $mini_cal.calendar -dateformat $::date_format -highlightcolor #30e8f0 -shadecolor LightSteelBlue3 -language $::language  -command [list [namespace current]::my Planer_Set_Working_Day $cal_main.tbl [$node getAttribute planer_tbl] $mini_cal.dd] -textvariable [namespace current]::working_date]
		
		pack [ttk::separator $mini_cal.sep -orient horizontal] -side top -padx 5 -pady 5 -expand 1 -fill x
		set planer_names_tbl [$node getAttribute planer_names_tbl]
		set ddvalues [lsort -dictionary [$::db select_column $planer_names_tbl name]]
		
		pack [ttk::combobox $mini_cal.dd -width 20 -state normal -values $ddvalues -textvariable [namespace current]::planer_name] -side left -padx 5 -pady 5

		bind $mini_cal.dd <<ComboboxSelected>> "[namespace current]::my planer_get_events [$node getAttribute planer_tbl] $cal_main.tbl %W"
		pack [ttk::button $mini_cal.remove_cal_name -image [image create photo -file [file join $img list-remove.png]] -command [list [namespace current]::my Planer_Remove_Cal $mini_cal.dd $planer_names_tbl]] -side right -padx 1 -pady 0
		tooltip::tooltip $mini_cal.remove_cal_name "[::msgcat::mc "Remove calendar"]"
		pack [ttk::button $mini_cal.add_cal_name -image [image create photo -file [file join $img list-add.png]] -command [list [namespace current]::my Planer_Add_Cal $mini_cal.dd $planer_names_tbl]] -side right -padx 1 -pady 0
		tooltip::tooltip $mini_cal.add_cal_name "[::msgcat::mc "Add calendar"]"

		pack [ttk::button $nav_b.one_next -image [image create photo -file [file join $img Button_Next.png]] -command [list [namespace current]::my Planer_Plus_One $cal_main.tbl [$node getAttribute planer_tbl] $mini_cal.dd]] -side right -padx 1 -pady 0
		tooltip::tooltip $nav_b.one_next "[::msgcat::mc "Next"]"
		pack [ttk::button $nav_b.today -image [image create photo -file [file join $img calendar_big.png]] -command [list [namespace current]::my Planer_Set_Working_Day $cal_main.tbl [$node getAttribute planer_tbl] $mini_cal.dd "[date]"]] -side right -padx 1 -pady 0
		tooltip::tooltip $nav_b.today "[::msgcat::mc "Today"]"
		pack [ttk::button $nav_b.one_back -image [image create photo -file [file join $img Button_Back.png]] -command [list [namespace current]::my Planer_Minus_One $cal_main.tbl [$node getAttribute planer_tbl] $mini_cal.dd]] -side right -padx 1 -pady 0
		tooltip::tooltip $nav_b.one_back "[::msgcat::mc "Back"]"

		dict append fdata [$node getAttribute var] $cal_main.tbl
		dict append fdata planer_name $mini_cal.dd
		dict append fdata current_date $nav_b.lbl
		my WidgetBind $node $cal_main.tbl
	} 
}

method planer_get_events {tbl tblist w} {
	set wd [set [namespace current]::working_date]
	set dtstart [clock format [clock scan $wd] -format %Y%m%d]
	set X_WR_CALNAME [$w get]
	set result_dict [$::db select_all $tbl * "\"X-WR-CALNAME\"='$X_WR_CALNAME' AND \"DTSTART\" LIKE '$dtstart%'"]
	
	set time_list [list 00:00 00:30 01:00 01:30 02:00 02:30 03:00 03:30 04:00 04:30 05:00 05:30 06:00 06:30 07:00 07:30 08:00 08:30 09:00 09:30 10:00 10:30 11:00 11:30 12:00 12:30 13:00 13:30 14:00 14:30 15:00 15:30 16:00 16:30 17:00 17:30 18:00 18:30 19:00 19:30 20:00 20:30 21:00 21:30 22:00 22:30 23:00 23:30]
	set times [dict create]
	$tblist delete 0 end

	for {set ti 0} {$ti < [llength $time_list]} {incr ti} {
		set time_cur [string map {: ""} [lindex $time_list $ti]]
		set time_next [string map {: ""} [lindex $time_list [expr $ti + 1]]]

		set last_t [lindex [dict keys $times] [expr [llength [dict keys $times]] - 1]]
		if {$last_t < [lindex $time_list $ti]} {
			dict append times [lindex $time_list $ti] ""
		}

		foreach dict_key [dict keys $result_dict] {
			set line [dict get $result_dict $dict_key]
			set dt [dict get $line DTSTART]
			set dtend [dict get $line DTEND]
			
			set t [::textutil::trimright [::textutil::trimleft $dt {[0-9]{8}T}] {00Z}]
			set time_num [lindex [::textutil::splitn $t 2] 0]:[lindex [::textutil::splitn $t 2] 1]
			set te [::textutil::trimright [::textutil::trimleft $dtend {[0-9]{8}T}] {00Z}]
			set time_num_end [lindex [::textutil::splitn $te 2] 0]:[lindex [::textutil::splitn $te 2] 1]
	
			if {$t >= $time_cur && $t < $time_next && $te <= $time_next} {
				set times [dict remove $times $time_num]
				dict append times "$time_num" "[dict get $line id]# [dict get $line ATTENDEE]-[dict get $line SUMMARY]"
				dict append times $time_num_end ""
				
			} elseif {$t >= $time_cur && $t < $time_next && $te >= $time_next} {
				set times [dict remove $times $time_num]
				dict append times "$time_num" "[dict get $line id]# [dict get $line ATTENDEE]-[dict get $line SUMMARY]"
				dict append times $time_num_end ""
			}
		}
	}

	foreach tl [dict keys $times] {
		$tblist insert end [list "$tl" "[dict get $times $tl]"]
	}
}

method Planer_Add_Cal {w tbl} {
	if {[$w get] ne ""} {
		$::db insert_all $tbl [dict create name [$w get]]
		set ddvalues [lsort -dictionary [$::db select_column $tbl name]]
		$w configure -values $ddvalues
	}
}

method Planer_Remove_Cal {w tbl} {
	set resp [tk_messageBox -icon warning -type okcancel -message "[::msgcat::mc "Are you sure you want to delete this calendar?"]"]
	if {$resp eq "ok"} {
		set id_to_del [$::db select_id_by_name $tbl [$w get]]
		$::db delete_row $tbl [lindex $id_to_del 0]
		set ddvalues [lsort -dictionary [$::db select_column $tbl name]]
		$w configure -values $ddvalues
	}
}

method Planer_Set_Working {d g} {
	set [namespace current]::working $d
	set working_what [set [namespace current]::working]
	if {$working_what eq "week"} {
		$g configure -columns "0 [::msgcat::mc "Time"] left 0 [::msgcat::mc "Monday"] left 0 [::msgcat::mc "Tuesday"] left 0 [::msgcat::mc "Wednesday"] 0 [::msgcat::mc "Thursday"] left 0 [::msgcat::mc "Friday"] left 0 [::msgcat::mc "Saturday"] left 0 [::msgcat::mc "Sunday"] left"
		set untranslated [clock format [clock scan [set [namespace current]::working_date]] -format %A]
		set translated [::msgcat::mc "$untranslated"]
		set col_count [$g columncount]
		for {set x 0} {$x < $col_count} {incr x} {
			set col_title [$g columncget $x -title]
			if {$col_title eq $translated} {
				$g columnconfigure $x -bg lightgreen
			} else {
				$g columnconfigure $x -bg white
			}
		}
	} elseif {$working_what eq "day"} {
		$g configure -columns "0 [::msgcat::mc "Time"] left 0 [::msgcat::mc "Description"] left"
		
	}
}

method Planer_Set_Working_Day {g tbl ddw w} {
	set [namespace current]::working_date $w
	set working_what [set [namespace current]::working]
	if {$working_what eq "week"} {
		set untranslated [clock format [clock scan [set [namespace current]::working_date]] -format %A]
		set translated [::msgcat::mc "$untranslated"]
		set col_count [$g columncount]
		for {set x 0} {$x < $col_count} {incr x} {
			set col_title [$g columncget $x -title]
			if {$col_title eq $translated} {
				$g columnconfigure $x -bg lightgreen
			} else {
				$g columnconfigure $x -bg white
			}
		}
	}
	my planer_get_events $tbl $g $ddw
}

method Planer_Plus_One {g tbl ddw} {
	set current_working [set [namespace current]::working_date]
	set working_what [set [namespace current]::working]
	set day_plus [clock format [clock add [clock scan $current_working] 1 $working_what]  -format $::date_format]
	set [namespace current]::working_date $day_plus
	if {$working_what eq "week"} {
		set untranslated [clock format [clock scan [set [namespace current]::working_date]] -format %A]
		set translated [::msgcat::mc "$untranslated"]
		set col_count [$g columncount]
		for {set x 0} {$x < $col_count} {incr x} {
			set col_title [$g columncget $x -title]
			if {$col_title eq $translated} {
				$g columnconfigure $x -bg lightgreen
			} else {
				$g columnconfigure $x -bg white
			}
		}
	}
	my planer_get_events $tbl $g $ddw
}

method Planer_Minus_One {g tbl ddw} {
	set current_working [set [namespace current]::working_date]
	set working_what [set [namespace current]::working]
	set day_plus [clock format [clock add [clock scan $current_working] -1 $working_what]  -format $::date_format]
	set [namespace current]::working_date $day_plus
	if {$working_what eq "week"} {
		set untranslated [clock format [clock scan [set [namespace current]::working_date]] -format %A]
		set translated [::msgcat::mc "$untranslated"]
		set col_count [$g columncount]
		for {set x 0} {$x < $col_count} {incr x} {
			set col_title [$g columncget $x -title]
			if {$col_title eq $translated} {
				$g columnconfigure $x -bg lightgreen
			} else {
				$g columnconfigure $x -bg white
			}
		}
	}
	my planer_get_events $tbl $g $ddw
}

method WidgetBind {node widget_name} {
	set bind_descp [list onEnter onDoubleClick onDelete onScan onSelect onFocusOut onKey]
	set bind_type [list <Return> <Double-1> <Delete> <Return> <<ComboboxSelected>> <FocusOut> <Key>]

	foreach descp $bind_descp type $bind_type {
		if {[$node hasAttribute $descp] != 0} {
			if {$descp eq "onKey"} {
				set subst_str {%W %K}
			} else {
				set subst_str "%W"
			}
			if {[string match -nocase *.ebin $widget_name] == 1} {
				bind $widget_name $type [list [$node getAttribute $descp] $subst_str]
			} elseif {[string match -nocase *.b $widget_name] == 1} {
				bind $widget_name $type [list [$node getAttribute $descp] $subst_str]
			} elseif {[string match -nocase *.lb $widget_name]} {
				bind $widget_name $type [list [$node getAttribute $descp] $subst_str]
			} elseif {[string match -nocase *.txt $widget_name]} {
				bind $widget_name $type [list [$node getAttribute $descp] $subst_str]
			} elseif {[string match -nocase *.picture $widget_name]} {
				bind $widget_name $type [list [$node getAttribute $descp] $subst_str]
			} elseif {[string match -nocase *.tbl $widget_name]} {
				bind [$widget_name bodytag] $type [list [$node getAttribute $descp] $subst_str]
			} else {
				bind $widget_name $type [list [$node getAttribute $descp] $subst_str]
			}
		}
	}
}






method Open_f {node types} {
	$node delete 0 end
	$node insert 0 [tk_getOpenFile -filetypes $types]
}

method Open_dir {node} {
	$node delete 0 end
	$node insert 0 [tk_chooseDirectory]
}

method  Common_attr {node attr} {
	my variable lsize
	my variable esize
	my variable width
	my variable height
	my variable justify

	if {[$node hasAttribute $attr] == 0} {
		return [set $attr]
	} else { 
		return [$node getAttribute $attr]
	}
}

method get_fdata {} {
	my variable fdata
	return $fdata
}


method get_values {{result_type dict} args} {
	my variable fdata
	set data [dict create]
	set widget_type [dict create]
	
	if {$::debug == "true"} {
		audit::audit GUI 4 "*********************************** Starting XML_WRAPPER debugging ***********************************"
		audit::audit GUI 4 "RESULT TYPE: $result_type"
		foreach var [dict keys $fdata] val [dict values $fdata] {
			audit::audit GUI 4 "FORM VARIABLE: $var FULL NAME: $val"
		} 
		audit::audit GUI 4 "*********************************** END of XML_WRAPPER debugging ***********************************"
	}

	foreach key [dict keys $fdata] value [dict values $fdata] {
		if {[string match -nocase *.ebin $value] == 1} {
			dict set widget_type $key ebin
			dict set data $key [$value get]
			
		} elseif {[string match -nocase *.b $value] == 1} {
			dict set widget_type $key b
			set a [set [set ::bool_var [$value cget -variable]]]
			dict set data $key $a

		} elseif {[string match -nocase *.lb $value]} {
			dict set widget_type $key lb
			set selected [$value curselection]
			if {$selected != ""} {
				set lbval [$value get $selected]
			} else {
				set lbval ""
			}
			dict set data $key $lbval
			
		} elseif {[string match -nocase *.lbl $value]} {
			dict set widget_type $key lbl
			set f [$value cget -textvariable]
			dict set data $key [expr $${f}]
			
		} elseif {[string match -nocase *.txt $value]} {
			dict set widget_type $key txt
			dict set data $key [$value get 1.0 {end -1c}]
		
		} elseif {[string match -nocase *.planer $value]} {
			
		} elseif {[string match -nocase *.dateperiod $value]} {
			dict set widget_type $key dateperiod
			set widget [::textutil::trim::trim $value .dateperiod]
			dict set data $key [compare_dates [$widget.date1 get] [$widget.date2 get]]

		} elseif {[string match -nocase *.radiob $value]} {
			dict set widget_type $key radiob
			set f [$value cget -variable]
			dict set data $key [expr $${f}]
	
		} elseif {[string match -nocase *.picture $value]} {
			dict set widget_type $key picture
			dict set data $key [$value cget -image]
	
		} elseif {[string match -nocase *.tbl $value]} {
			dict set widget_type $key tbl
			set column_names [list]
			for {set col_index 0} {$col_index < [$value columncount]} {incr col_index} {
				lappend column_names [$value columncget $col_index -name]
			}

			set lines_indexes [$value curselection]
			set tbl_data ""

			for {set line_num 0} {$line_num < [llength $lines_indexes]} {incr line_num} {
				set line_val [$value get [lindex $lines_indexes $line_num]]
				for {set r 0} {$r < [llength $line_val]} {incr r} {
					dict set data_line [$value columncget $r -name] [lindex $line_val $r]
				}
				dict set tbl_data $line_num $data_line
			}
			dict set data $key $tbl_data

		} else {
			dict set widget_type $key normal
			dict set data $key [$value get]
		}
	}

	if {[llength $args] != 0 } {
		if {$result_type eq "dict"} {
			foreach key [dict keys $data] val [dict values $data] {
				if {[lsearch $args $key ] != -1} {
					dict set result $key $val
				}
			}
		} elseif {$result_type eq "list"} {
			set result [list]
			foreach key [dict keys $data] val [dict values $data] {
				if {[lsearch $args $key ] != -1} {
					if {[dict get $widget_type $key] ne "tbl"} {
						lappend result $val
					} elseif {[dict get $widget_type $key] eq "tbl"} {
						set tbl_lines ""
						foreach line_num [dict keys $val] {
							set line_val [dict get $val $line_num]
							lappend tbl_lines [dict values $line_val]
						}
						lappend result $tbl_lines
					}
				}
			}
		}
	} else {
		set result $data
	}
	return $result
}

method clear_values {{to_clear_list {}}} {
	my variable fdata
	set widget_var [dict keys $fdata]
	set widget_full_name [dict values $fdata]

	for {set i 0} {$i < [llength $widget_full_name]} {incr i} {
		if {[string match -nocase *.b [lindex $widget_full_name $i]] == 1} {
			if {[llength $to_clear_list] != 0 && [lsearch -exact $to_clear_list [lindex $widget_var $i]] != "-1"} {
				[lindex $widget_full_name $i] state alternate
			} elseif {[llength $to_clear_list] == 0} {
				[lindex $widget_full_name $i] state alternate
			}
		} elseif {[string match -nocase *.tbl [lindex $widget_full_name $i]]} {
			if {[llength $to_clear_list] != 0 && [lsearch -exact $to_clear_list [lindex $widget_var $i]] != "-1"} {
				[lindex $widget_full_name $i] delete 0 end
			} elseif {[llength $to_clear_list] == 0} {
				[lindex $widget_full_name $i] delete 0 end
			}
		} elseif {[string match -nocase *.txt [lindex $widget_full_name $i]]} {
			if {[llength $to_clear_list] != 0 && [lsearch -exact $to_clear_list [lindex $widget_var $i]] != "-1"} {
				[lindex $widget_full_name $i] delete 1.0 end
			} elseif {[llength $to_clear_list] == 0} {
				[lindex $widget_full_name $i] delete 1.0 end
			}
		} elseif {[string match -nocase *.lb [lindex $widget_full_name $i]]} {
			if {[llength $to_clear_list] != 0 && [lsearch -exact $to_clear_list [lindex $widget_var $i]] != "-1"} {
				[lindex $widget_full_name $i] selection clear 0 end
			} elseif {[llength $to_clear_list] == 0} {
				[lindex $widget_full_name $i] selection clear 0 end
			}
		} elseif {[string match -nocase *.picture [lindex $widget_full_name $i]]} {

		} elseif {[string match -nocase *.dd [lindex $widget_full_name $i]]} {
			if {[llength $to_clear_list] != 0 && [lsearch -exact $to_clear_list [lindex $widget_var $i]] != "-1"} {
				[lindex $widget_full_name $i] current 0
			} elseif {[llength $to_clear_list] == 0} {
				[lindex $widget_full_name $i] current  0
			}
		} elseif {[string match -nocase *.dateperiod [lindex $widget_full_name $i]]} {
			if {[llength $to_clear_list] != 0 && [lsearch -exact $to_clear_list [lindex $widget_var $i]] != "-1"} {
				set widget [::textutil::trim::trim [lindex $widget_full_name $i] .dateperiod]
				$widget.date1 delete 0 end
				$widget.date2 delete 0 end
			} elseif {[llength $to_clear_list] == 0} {
				set widget [::textutil::trim::trim [lindex $widget_full_name $i] .dateperiod]
				$widget.date1 delete 0 end
				$widget.date2 delete 0 end
			}
		} elseif {[string match -nocase *.radiob [lindex $widget_full_name $i]]} {
			if {[llength $to_clear_list] != 0 && [lsearch -exact $to_clear_list [lindex $widget_var $i]] != "-1"} {
				set [namespace current]::[lindex $widget_var $i]  ""
			} elseif {[llength $to_clear_list] == 0} {
				set [namespace current]::[lindex $widget_var $i]  ""
			}
		} else {
			if {[llength $to_clear_list] != 0 && [lsearch -exact $to_clear_list [lindex $widget_var $i]] != "-1"} {
				[lindex $widget_full_name $i] delete 0 end
			} elseif {[llength $to_clear_list] == 0} {
				[lindex $widget_full_name $i] delete 0 end
			}
		}
	}
}

method Check_sdata {var} {
	if {[info exists $sdata] == 1} {
		set val [lsearch -all [dict keys $sdata] $var]
		if {$val != ""} {
			return [lindex [dict values $sdata] $val]
		} else {
			return ""
		}	
	} else {
		return ""
	}
}

method new_form_values {data_to_ins} {
	my variable fdata
	foreach key [dict keys $data_to_ins] val [dict values $data_to_ins] {
		if {[dict exists $fdata $key] != 0} {
			set to_up [dict get $fdata $key]
			if {[string match -nocase *.dd [dict get $fdata $key]]} {
				$to_up configure -values $val
			}
		}
	}
}

# Need add check if value not in a dict select some default or empty

method update_form {data_to_ins} {
	my variable fdata

	# Check if dictionary has table_action key, if not we assuming to clean table
	if {[dict exists $data_to_ins table_clear] == 0} {
		dict lappend data_to_ins table_clear 1
	}

	foreach key [dict keys $data_to_ins] val [dict values $data_to_ins] {
		if {[dict exists $fdata $key] != 0} {
			set to_up [dict get $fdata $key]
			if {[string match -nocase *.tbl [dict get $fdata $key]]} {
				if {[dict get $data_to_ins table_clear] == 1} {
					$to_up delete 0 end
				}
				foreach dict_index [dict keys $val] {
					set l [dict get $val $dict_index]
					$to_up insert end [dict values $l]
				}
			} elseif {[string match -nocase *.b [dict get $fdata $key]]} {
				if {$val eq "t" || $val == 1} {
					$to_up invoke
					# $to_up invoke
				}
			} elseif {[string match -nocase *.dd [dict get $fdata $key]]} {
				if {[set index [lsearch -exact [$to_up cget -values] [string trim $val]]] != -1} {
					$to_up current $index
				} else {
					$to_up current 0
				}
			} elseif {[string match -nocase *.txt [dict get $fdata $key]]} {
				$to_up delete 1.0 end
				$to_up insert 1.0 $val
			
			} elseif {[string match -nocase *.picture [dict get $fdata $key]]} {
				$to_up configure -image $val
				
			} elseif {[string match -nocase *.radiob [dict get $fdata $key]]} {
				set var [$to_up cget -variable]
				set $var $val

			} elseif {[string match -nocase *.time [dict get $fdata $key]]} {
				set [[dict get $fdata $key] cget -textvariable] $val
				
			} else {
				if {[$to_up cget -state] == "disabled"} {
					$to_up configure -state normal
					$to_up delete 0 end
					$to_up insert 0 $val
					$to_up configure -state disabled
				}
				$to_up delete 0 end
				$to_up insert 0 $val
			}
		}
	}
}

method state_widget {widgets {state ""} {clear 0}} {
	my variable fdata

	foreach widget_name $widgets {
		set widget [dict get $fdata $widget_name]
		
		if {$state eq ""} {
			set current_state [$widget cget -state]
			if {$current_state eq "normal" || $current_state eq "readonly"} {
				if {$clear ne 0} {
					my clear_values $widget_name
				}
				$widget configure -state disabled
			} else {
				if {[string match -nocase *.dd $widget]} {
					$widget configure -state readonly
				} else {
					$widget configure -state normal
				}
			}
		} else {
			if {$clear ne 0} {
				my clear_values $widget_name
			}
			$widget configure -state $state
		}
	}
}

method sum_tablelist_column {table_var column} {
	my variable fdata
	
	set values_list [[dict get $fdata $table_var] getcolumns $column]
	set count 0
	foreach val $values_list {
		if {[lindex $values_list $count] eq ""} {
			set values_list [lreplace $values_list $count $count 0.0]
		}
		set count [expr $count + 1]
	}
	return [::tcl::mathop::+ {*}$values_list]
}













































} ;#End of class definition